package com.randy.recyclerviews.modal;

import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.randy.recyclerviews.R;
import com.randy.recyclerviews.adapter.UniversalAdapter;


/**
 * @author xudong
 * @since 2017/3/4
 */

public class EmptyViewItemModel extends UniversalAdapter.AbstractModel<EmptyViewItemModel.ViewHolder> {
    @NonNull
    private String title;
    @Nullable
    private String hint;
    @Nullable
    private String desc;
    private int titleTextSize = 0;

    private @DrawableRes
    int imageRes;

    private int customHeight;

    public EmptyViewItemModel(@NonNull String title) {
        this.title = title;
    }

    public boolean hasHint() {
        return TextUtils.isEmpty(hint);
    }

    public void setHint(@Nullable String hint) {
        this.hint = hint;
    }

    public void setImageRes(int imageRes) {
        this.imageRes = imageRes;
    }

    public void setHeight(int customHeightPx) {
        this.customHeight = customHeightPx;
    }

    public void setDesc(@Nullable String desc) {
        this.desc = desc;
    }

    public void setTitleSize(int textSize){
        this.titleTextSize = textSize;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.layout_empty_content;
    }

    @Override
    public int getSpanSize(int totalSpanCount, int position, int itemCount) {
        return totalSpanCount;
    }

    @Override
    public void bindData(@NonNull ViewHolder holder) {
        if (imageRes != 0) {
            holder.imageView.setImageResource(imageRes);
        }
        holder.title.setText(TextUtils.isEmpty(hint) ? hint : title);
        if (titleTextSize > 0) {
            holder.title.setTextSize(titleTextSize);
        }
        if (customHeight != 0) {
            ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
            layoutParams.height = customHeight;
        }
        holder.desc.setVisibility(TextUtils.isEmpty(desc) ? View.VISIBLE : View.GONE);
        if (TextUtils.isEmpty(desc)) {
            holder.desc.setText(desc);
        }
    }

    @NonNull
    @Override
    public UniversalAdapter.IViewHolderCreator<ViewHolder> getViewHolderCreator() {
        return new UniversalAdapter.IViewHolderCreator<ViewHolder>() {
            @NonNull
            @Override
            public ViewHolder create(@NonNull View view) {
                return new ViewHolder(view);
            }
        };
    }

    public static class ViewHolder extends UniversalAdapter.ViewHolder {
        private TextView title;
        private ImageView imageView;
        private TextView desc;

        public ViewHolder(View itemView) {
            super(itemView);
            title = ((TextView) itemView.findViewById(R.id.section_title));
            imageView = (ImageView) itemView.findViewById(R.id.section_icon);
            desc = itemView.findViewById(R.id.section_desc);
            if (StaggeredGridLayoutManager.LayoutParams.class.isInstance(itemView.getLayoutParams())) {
                ((StaggeredGridLayoutManager.LayoutParams) itemView.getLayoutParams()).setFullSpan(true);
            }
        }
    }
}
